package titt.io.socket;

import java.net.Socket;

import titt.io.IOFactory;
import titt.io.writer.head.HeadWriter;
import titt.io.reader.head.HeadReader;
import titt.io.socket.in.TITTInputStream;
import titt.io.socket.out.TITTOutputStream;

public class TITTSocketImpl implements TITTSocket {

	private HeadReader hr;
	private HeadWriter hw;
	private final TITTInputStream in;
	private final TITTOutputStream out;
	private final Socket s;
	
	public TITTSocketImpl(Socket s) throws Exception {
		
		this.in = IOFactory.createInputStream(s.getInputStream());
		this.out = IOFactory.createOutputStream(s.getOutputStream());
		hr = IOFactory.createHeadReader(this.in);
		hw = IOFactory.createHeadWriter(this.out);
		
		this.s = s;
	}
	
	@Override
	public void setTimeout(int timeout) throws Exception {
		s.setSoTimeout(timeout);
	}
		
	@Override
	public HeadReader getHeadReader() {
		return hr;
	}

	@Override
	public HeadWriter getHeadWriter() {
		return hw;
	}
	
	@Override
	public TITTInputStream getInputStream() {
		return in;
	}

	@Override
	public TITTOutputStream getOutputStream() {
		return out;
	}

	@Override
	public void close() throws Exception {
		if(s != null) s.close();
		in.close();
		out.close();
	}
	
	@Override
	public boolean isClosed() {
		return s.isClosed();
	}
}
